package mamikeji.liaowa.ui.adapter;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;

import java.util.ArrayList;
import java.util.List;

import mamikeji.liaowa.R;
import mamikeji.liaowa.bean.CollectionListBean;
import mamikeji.liaowa.widget.slidelistview.SliderView;

/**
 * Created by 廉冲-android开发 on 2017/6/14.
 */
public class CollectionAdapter extends BaseAdapter {
    List<CollectionListBean.DataEntity> mList = new ArrayList<>();
    LayoutInflater inflater;
    Context context;
    OnItemClickListener mOnItemClickListener;

    SliderView slideView;

    public CollectionAdapter() {
    }

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public interface OnItemClickListener {
        void onDeleteClick(int id);
    }

    public CollectionAdapter(List<CollectionListBean.DataEntity> mList, Context context) {
        this.mList = mList;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    public void setList(List<CollectionListBean.DataEntity> mList) {
        this.mList = mList;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public CollectionListBean.DataEntity getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = new ViewHolder();
        slideView = (SliderView) convertView;
        if (slideView == null) {
            convertView = inflater.inflate(R.layout.item_collection_activity, null);

            viewHolder.textView_collection_num = (TextView) convertView.findViewById(R.id.textView_collection_num);
            viewHolder.textView_collection_title = (TextView) convertView.findViewById(R.id.textView_collection_title);
            viewHolder.textView_collection_content = (TextView) convertView.findViewById(R.id.textView_collection_content);
            viewHolder.imageView_collection = (SimpleDraweeView) convertView.findViewById(R.id.imageView_collection);

            slideView = new SliderView(context);
            slideView.setContentView(convertView);
            slideView.setTag(viewHolder);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) slideView.getTag();
        }

        CollectionListBean.DataEntity dataEntity = getItem(position);

        slideView.shrink();

        viewHolder.textView_collection_num.setText(position + 1 + "");
        viewHolder.textView_collection_title.setText(dataEntity.getTitle());
        viewHolder.textView_collection_content.setText(dataEntity.getContent());

        viewHolder.imageView_collection.setImageURI(Uri.parse(dataEntity.getImgUrl()));

        slideView.setOnItemClickListener(new SliderView.OnItemClickListener() {
            @Override
            public void onDeleteClick() {
                mOnItemClickListener.onDeleteClick(position);
            }
        });
        return slideView;
    }

    public class ViewHolder {
        TextView textView_collection_num;
        TextView textView_collection_title;
        TextView textView_collection_content;
        SimpleDraweeView imageView_collection;
    }

}
